/*
 * Attempt to solve the "Homework" problem.
 * http://www.iarcs.org.in/inoi/contests/jun2006/Advanced-2.php
 *
 */
#include "stdafx.h"
using namespace std;

struct hwproblem {
	int deadline;
	int bonus;
};

/* Function object; binary predicate.
 * 
 * Defines an ordering for hw problems:
 * 1. earlier deadline < later deadline (main criteria)
 * 2. more bonus < less bonus (tiebreaker)
 */
class hw_prob_compare  {
public:
	bool operator() (hwproblem a, hwproblem b)  {
		if(a.deadline < b.deadline) return true;
		else
			if(a.deadline > b.deadline) return false;
			else return (a.bonus > b.bonus);
	}
};

void homework_main() {
	int nproblems;
	cin >> nproblems;

	// input the bonus points and their deadlines
	vector<hwproblem> probs(nproblems);
	for(int i = 0; i < nproblems; i++)
		cin >> probs[i].deadline >> probs[i].bonus;

	// sort the problems using our cool predicate
	sort(probs.begin(), probs.end(), hw_prob_compare());

	typedef vector<hwproblem>::iterator prob_itr;
	prob_itr prob = probs.begin();

	int total_bonus_points = 0;
	int day = 1;
	for(; prob != probs.end(); prob++)  {
		if(prob->deadline >= day)  {
			total_bonus_points += prob->bonus;
			day++;
		}
	}
	cout << total_bonus_points;
};